package Server;

import java.net.InetAddress;
import java.rmi.*;
import java.rmi.registry.LocateRegistry;

import RMI.InterfazRemota;
import RMI.ObjetoRemoto;


public class Server {
    
	InterfazRemota objetoRemoto;
	
	public Server() {
		ServerRun.server = this;
	}
	
    public void iniciar() {
    	try {
    		LocateRegistry.createRegistry(1099);
            InterfazRemota objetoRemoto = new ObjetoRemoto();
            Naming.rebind ("//localhost/ObjetoRemoto",(Remote) objetoRemoto);
            verVinculos();
		} catch (Exception e) {
			ServerRun.panelCentral.getDisplay().setText(e.getMessage());
		}
    }
    
    public void verVinculos() {
        try {
      	  String[] vinculos = Naming.list( "" );
      	  ServerRun.panelCentral.getDisplay().setText("Vinculos disponibles en el servidor: ");
      	  ServerRun.panelCentral.getDisplay().append(InetAddress.getLocalHost().getHostAddress());
      	  for ( int i = 0; i < vinculos.length; i++ ){
      		ServerRun.panelCentral.getDisplay().append( "\n"+vinculos[i] );
      	  }
        }
        catch (Exception e) {
      	  e.printStackTrace();
        }
     }
    
    public void cerrar() {
		try {
			Naming.unbind("//localhost/ObjetoRemoto");
		} catch (Exception e) {
		} finally {
			ServerRun.cerrarVentana();
		}
    }
}
